Navigation

  • index
  • next |
  • previous |
  • PyHowTo documentation »
  • Basic »
  • Searches »

Table of Contents

Python v3.7 HowTos:

  • ----------------
  • Recursion
  • Backtracking
  • Dynamic Programming
  • Greedy
  • Sort
  • Binary Search
  • Depth First Search [DFS]
  • Breadth First Search [BFS]
  • Binary Search Tree [BST]
  • ----------------
  • Array
  • String
  • Heap
  • Stack
  • Queue
  • Tree
  • Linked List
  • Hash Table
  • Bit Manipulation
  • Two Pointers
  • Math
  • Decorator
  • ----------------
  • Basic
  • Intermediate
  • Advanced
  • Interview
  • ----------------
  • Spark
  • Tkinter
  • Turtle
  • Games
  • Web
  • ----------------
  • About
  • History

Previous topic

Binary search for an ORDERED list (only)

Next topic

String

Quick search

Sequential searchΒΆ

Write a python program for Sequential Search.
Sequential Search : In computer science, linear search or sequential search
is a method for finding a particular value in a list that checks each element
in sequence until the desired element is found or the list is exhausted.
The list need not be ordered.
Test Data :
sequential_search([11,23,58,31,56,77,43,12,65,19],31)
Expected Result:
(True, 3)
def sequential_search(A, N):

    idx = 0
    found = False

    while idx < len(A) and not found:
        if A[idx] == N:
            found = True
        else:
            idx = idx + 1

    return found, idx

print(sequential_search([11, 23, 58, 31, 56, 77, 43, 12, 65, 19], 31))

Output:

(True, 3)

See also

https://www.w3resource.com/python-exercises/data-structures-and-algorithms/python-search-and-sorting-exercise-2.php

Navigation

  • index
  • next |
  • previous |
  • PyHowTo documentation »
  • Basic »
  • Searches »
© Copyright 2020, Sergiy Zaytsev, szaytsev@hotmail.com. Created using Sphinx 2.3.0.